package com.angel.controller;

import com.angel.bean.UserInfo;
import com.angel.config.MyConfig;
import com.angel.service.UserInfoService;
import com.angel.utils.MessageHttpUtils;
import com.angel.utils.Random;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户注册
 */
@Controller
@RequestMapping("/r")
public class UserRegisterController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private MyConfig myConfig;

    /**
     * 注册界面
     *
     * @return
     */
    @GetMapping("/signup")
    public String sign_up() {
        return "sign-up-foreend";
    }

    /**
     * 校验用户的对应字段是否存在
     *
     * @param userInfo
     * @return
     */
    @RequestMapping("/isexist")
    @ResponseBody
    public Object isExits(UserInfo userInfo) {
        Map<String, Object> map = new HashMap<>();
        //初始化默认是用户信息是不存在的
        map.put("emp", false);
        UserInfo result = userInfoService.findOne(userInfo);
        //如果查询出来的结果为空，则返回空
        if (result == null) {
            map.put("emp", true);
        }
        return map;
    }


    /**
     * 注册
     */
    @RequestMapping("/Reg")
    @ResponseBody
    public Object register(UserInfo userInfo) {
        Map<String, Object> map = new HashMap<>();
        if (userInfo != null) {
            userInfo.setState(1);
            userInfo.setUserName(userInfo.getUserName().replace(" ", ""));
            userInfo.setUserMail(userInfo.getUserMail().replace(" ", ""));
            userInfo.setPassword(userInfo.getPassword().replace(" ", ""));
            userInfo.setUserAddress(userInfo.getUserAddress().replace(" ", ""));
            userInfo.setLoginDate(new Timestamp(System.currentTimeMillis()));
            userInfo.setSumCharge("0");
            //设置注册时默认为会员用户（分会员用户和学员）
            userInfo.setReserve1("0");
            int result = userInfoService.insert(userInfo);
            if (result != 0) {
                map.put("emp",true);
            } else {
                map.put("emp", false);
            }
        }
        return map;
    }

    /**
     * 获取验证码
     *
     * @param userTel 电话号码
     */
    @RequestMapping("/message")
    @ResponseBody
    public Object message(String userTel) {
        Map<String, Object> map = new HashMap<>();
        //
        Random random = new Random();
        //随机生成验证码
        String str = random.digit(4);
        System.out.println(str);
        String msg = MessageHttpUtils.sendMessage(userTel, str, myConfig.getPtl_id(), myConfig.getAppcode());
        JSONObject jsonObject = new JSONObject(msg);
        System.out.println(jsonObject.getString("return_code")+"----------------------");
        if (jsonObject.getString("return_code").equals("00000")) {
            map.put("emp", true);
            map.put("message", str);
        } else {
            map.put("emp", false);
            map.put("message", "短信发送失败");
        }
        return map;
    }
}
